﻿using Flare.DB.CeEntity;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Flare.Pages.ITContract
{
    public partial class Service : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Услуги"; }
        }

        public Service()
        {
            InitializeComponent();
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            ServiceList.SelectedItem = null;
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new contract_service()
            {
                service = service.Text
            };

            try
            {
                new DB.contract_services().Create(data);
                service.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("contract_servicesDataSet");
        }

        private void ContractList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ServiceList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (ServiceList.SelectedIndex == -1) return;
            var data = new contract_service()
            {
                id_service = (int)ServiceList.SelectedValue,
                service = service.Text
            };

            try
            {
                new DB.contract_services().Edit(data);
                ServiceList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }
            glob.kernel.RefreshResourceByName("contract_servicesDataSet");
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (ServiceList.SelectedIndex == -1) return;
            try
            {
                new DB.contract_services().Delete((int)ServiceList.SelectedValue);
                ServiceList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }
            glob.kernel.RefreshResourceByName("contract_servicesDataSet");
        }
    }
}
